
/**
 * 1.字符串补位  -传递来的数字长度不足2位就左侧补充0
 * @param {string||number} data  比如:时分1==》01
 * @returns 补位后的数据
 */
function zeroize(data) {
    return String(data).length == 1 ? '0' + data : data
}

/**
 * 2.获取当前时间
 * @param {string} str   比如'Y-m-d H：i：s'
 * @returns string
 */
function format(str) {
    var nowdate = new Date()
    str = str.replaceAll('Y', nowdate.getFullYear())
    str = str.replaceAll('m', zeroize(nowdate.getMonth() + 1))
    str = str.replaceAll('d', zeroize(nowdate.getDate()))
    str = str.replaceAll('H', zeroize(nowdate.getHours()))
    str = str.replaceAll('i', zeroize(nowdate.getMinutes()))
    str = str.replaceAll('s', zeroize(nowdate.getSeconds()))
    return str
}

/**
 * 3.日期格式化
 * 输出格式：21小时前
 * stringTime为:年-月-日 时:分:秒
 * @param  {[type]} time [description]
 * @return {[type]}      [description]
*/
function formatTime(stringTime) {
    let milliScounds = 1000;
    let minute = 1000 * 60;
    let hour = minute * 60;
    let day = hour * 24;
    let week = day * 7;
    let month = day * 30;
    let time1 = new Date().getTime(); //当前的时间戳
    //let time2 = Date.parse(new Date(stringTime)); //指定时间的时间戳
    let time2 = new Date(stringTime).getTime(); //指定时间的时间戳
    let time = time1 - time2;

    let result = null;
    if (time < 0) {
        result = "--";
    } else if (time / month >= 1) {
        result = parseInt(time / month) + "月前";
    } else if (time / week >= 1) {
        result = parseInt(time / week) + "周前";
    } else if (time / day >= 1) {
        result = parseInt(time / day) + "天前";
    } else if (time / hour >= 1) {
        result = parseInt(time / hour) + "小时前";
    } else if (time / minute >= 1) {
        result = parseInt(time / minute) + "分钟前";
    } else {
        result = "刚刚";
    }
    return result;
}

/**
 * 4.随机颜色
 * @returns 随机生成的颜色 
 */
function randomColor() {
    var arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F']
    var max = arr.length - 1
    var min = 0
    var colorIndex = '#'
    for (i = 0; i < 6; i++) {
        colorIndex += arr[Math.floor(Math.random() * (max - min + 1) + min)]
    }
    return colorIndex
}

/**
 * 5.浏览器内核判断   edition版本
 */
function edition() {
    var agent = navigator.userAgent.toLowerCase()
    if (agent.indexOf('applewebkit') > -1) {
        return '当前是苹果，谷歌内核'
    } else if (agent.indexOf('trident') !== -1) {
        return '当前是IE内核'
    } else if (agent.indexOf('presto') !== -1) {
        return '当前是Opera内核'
    } else if (agent.indexOf('gecko') !== -1 && agent.indexOf('khtml') == -1) {
        return '当前是火狐内核'
    }

}

/**
 * 6.PC 移动判断
 */

function mobile() {
    var agent = navigator.userAgent.toLowerCase()
    if (agent.indexOf('mobile') !== -1) {
        return '当前是移动端'
    } else {
        return '当前是PC端'
    }
}

/**
 * 7.判断是否是微信浏览器
 */
function weixin() {
    var agent = navigator.userAgent.toLowerCase()
    if (agent.indexOf('micromessenger') !== -1) {
        return '当前是微信浏览器'
    } else {
        return '当前是其他浏览器'
    }
}

/**
 * 8.版本兼容获取css属性
 * @param {标签对象}  element (tagObj) document.querySelector('标签对象')
 * @param {string} cssAttr css属性
 * @returns css属性值
 */
function getStyle(tagObj, cssAttr) {
    if (window.getComputedStyle) {
        return getComputedStyle(tagObj)[cssAttr]
    } else {
        return tagObj.currentStyle[cssAttr]
    }
}














